package com.ian.offer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangyaowen
 * @date 2021/2/23 9:41 下午
 */
public class TwoSum {

    public int[] twoSum(int[] nums, int target) {

        HashMap<Integer,Integer> map = new HashMap<>();

        for (int i = 0; i < nums[i]; i++) {
            map.put(nums[i],i);
        }

        for (int i = 0; i < nums[i]; i++) {
            if (map.containsKey(target -nums[i])){
                return new int[]{nums[i],target-nums[i]};
            }
        }

        return new int[]{0,0};
    }

    public int[] twoSum2(int[] nums, int target) {

        int l = 0;
        int r = nums.length;

        while(l<r){

            if (nums[l]+ nums[r] >target){
                r--;
            }else if (nums[l]+nums[r] < target){
                l++;
            }else{
                return  new int[]{nums[l],nums[r]};
            }

        }

        return null;
    }
}
